import Vue from "vue";
import Router from "vue-router";
import store from "@/store";
import i18n from "../i18n";
// import { Message } from "element-ui";
// import Cookie from "@u/cookie";
Vue.use(Router);

import Layout from "@/layout";
import getTitle from "@u/getTitle";

/**
 * 路由相关属性说明
 * hidden: 当设置hidden为true时，意思不在sideBars侧边栏中显示
 * mete{
 * title: xxx,  设置sideBars侧边栏名称
 * icon: xxx,  设置ideBars侧边栏图标
 * noCache: true  当设置为true时不缓存该路由页面
 * }
 */

/*通用routers*/
export const currencyRoutes = [
  {
    path: "/login",
    name: "Login",
    component: () => import("@v/login"),
    meta: { title: "登录" },
    hidden: true
  },
  {
    path: "/404",
    name: "404",
    component: () => import("@v/error-page/404.vue"),
    hidden: true
  },
  {
    path: "/",
    name: "Home",
    component: Layout,
    redirect: "/dashbord",
    children: [
      {
        path: "dashbord",
        name: "Dashbord",
        component: () => import("@v/dashboard"),
        meta: { title: "slider.首页", icon: "el-icon-s-data" }
      }
    ]
  }
];
/*动态添加routers*/
export const asyncRoutes = [
  {
    path: "/member",
    name: "Member",
    redirect: "/member/index",
    component: Layout,
    // meta: {
    //   title: "slider.会员管理",
    //   icon: "el-icon-table iconfont"
    // },
    children: [
      {
        path: "index",
        name: "Member-index",
        component: () => import("@v/member/index"),
        meta: { title: "slider.会员管理", icon: "el-icon-table iconfont" }
      }
    ]
  },
  {
    path: "/feed",
    component: Layout,
    name: "Feed",
    redirect: "/feed/index",
    meta: { icon: "el-icon-coin", title: "slider.Feed" },
    children: [
      {
        path: "index",
        name: "Feed-index",
        component: () => import("@v/feed/index"),
        meta: { icon: "el-icon-table iconfont", title: "slider.Feed列表" }
      }
    ]
  },
  {
    path: "/diary",
    component: Layout,
    name: "Diary",
    redirect: "/diary/index",
    // meta: { icon: "el-icon-s-marketing", title: "slider.感谢日记管理" },
    children: [
      {
        path: "index",
        name: "Diary-index",
        component: () => import("@v/diary/index"),
        meta: { icon: "el-icon-table iconfont", title: "slider.感谢日记管理" }
      }
    ]
  },
  {
    path: "/thankCard",
    component: Layout,
    name: "ThankCard",
    redirect: "/thankCard/index",
    // meta: { icon: "el-icon-table iconfont", title: "slider.感谢卡管理" },
    children: [
      {
        path: "index",
        name: "ThankCard-index",
        component: () => import("@v/thankCard/index"),
        meta: { icon: "el-icon-table iconfont", title: "slider.感谢卡管理" }
      }
    ]
  },
  {
    path: "/eventRecruit",
    component: Layout,
    name: "EventRecruit",
    redirect: "/eventRecruit/index",
    // meta: { icon: "el-icon-table iconfont", title: "slider.活动募集管理" },
    children: [
      {
        path: "index",
        name: "EventRecruit-index",
        component: () => import("@v/eventRecruit/index"),
        meta: { icon: "el-icon-table iconfont", title: "slider.活动募集管理" }
      }
    ]
  },
  {
    path: "/eventPostscript",
    component: Layout,
    name: "EventPostscript",
    redirect: "/eventPostscript/index",
    // meta: { icon: "el-icon-table iconfont", title: "slider.活动后记管理" },
    children: [
      {
        path: "index",
        name: "EventPostscript-index",
        component: () => import("@v/eventPostscript/index"),
        meta: { icon: "el-icon-table iconfont", title: "slider.活动后记管理" }
      }
    ]
  },
  {
    path: "/medal",
    component: Layout,
    name: "Medal",
    redirect: "/medal/index",
    // meta: { icon: "el-icon-table iconfont", title: "slider.勋章管理" },
    children: [
      {
        path: "index",
        name: "Medal-index",
        component: () => import("@v/medal/index"),
        meta: { icon: "el-icon-table iconfont", title: "slider.勋章管理" }
      }
    ]
  },
  {
    path: "/serveRule",
    component: Layout,
    name: "ServeRule",
    redirect: "/serveRule/index",
    // meta: { icon: "el-icon-table iconfont", title: "slider.服务条例管理" },
    children: [
      {
        path: "index",
        name: "ServeRule-index",
        component: () => import("@v/serveRule/index"),
        meta: { icon: "el-icon-table iconfont", title: "slider.服务条例管理" }
      }
    ]
  },
  {
    path: "/permission",
    name: "Permission",
    component: Layout,
    redirect: "/permission/page-use",
    meta: {
      title: "slider.权限",
      icon: "el-icon-lock"
    },
    children: [
      {
        path: "page-user",
        name: "PageUser",
        component: () => import("@v/permission/page-user"),
        meta: { title: "slider.管理员列表", icon: "el-icon-user" }
      },
      {
        path: "roles",
        name: "Roles",
        component: () => import("@v/permission/roles"),
        meta: { title: "slider.权限设置", icon: "el-icon-s-tools" }
      }
    ]
  }
];
const creatRouter = () => {
  return new Router({
    routes: currencyRoutes,
    scrollBehavior() {
      return { x: 0, y: 0 };
    }
  });
};
const router = creatRouter();
// 解决addRoute不能删除动态路由问题
export function resetRouter() {
  const reset = creatRouter();
  router.matcher = reset.matcher;
}

// 导航守卫
router.beforeEach(async (to, from, next) => {
  document.title = getTitle(i18n.t(to.meta.title + ""));
  if (to.path === "/login") {
    next();
  } else {
    if (store.getters.token) {
      const hasRoles = store.getters.addRoutes.length > 0;
      if (hasRoles || store.getters.isFindRouter) {
        next();
      } else {
        var roles = store.getters.roles;
        const addRoutes = await store.dispatch(
          "permission/getAsyncRoutes",
          roles
        );
        // window.
        router.addRoutes(addRoutes);
        // hack method to ensure that addRoutes is complete
        // set the replace: true, so the navigation will not leave a history record
        next({ ...to, replace: true });
      }
    } else {
      next({
        path: "/login",
        query: {
          redirect: to.fullPath
        }
      });
    }
  }
});

export default router;
